//
//  StatusViewModel.swift
//  DSWeiBo
//
//  Created by 刘星 on 16/8/1.
//  Copyright © 2016年 LiuXing. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    // MARK:- 定义属性
    var status: Status?
    /// 微博来源处理
    var sourceText: String?
    /// 微博时间处理
    var createAtText : String?
    /// 微博认证
    var verifiedImage : UIImage?
    /// 微博Vip头像
    var vipImage : UIImage?
    /// 处理用户头像的地址
    var profileURL : NSURL?
    /// 处理微博配图的数据
    var picURLs : [NSURL] = [NSURL]()
    var cellHeigth: CGFloat = 0
    
    // MARK:- 自定义方法
    init(status: Status) {
        self.status = status
        // 1.来源
        if let source = status.source where source != "" {
            // 获取起始位置和截取的长度
            let startIndex = (source as NSString).rangeOfString(">").location + 1
            let length = (source as NSString).rangeOfString("</").location - startIndex
            sourceText = (source as NSString).substringWithRange(NSRange(location: startIndex, length: length))
        }
        // 2.时间
        if let created_at = status.created_at {
            createAtText = NSDate.createDateString(created_at)
        }
        // 3.认证
        let verified_type = status.user?.verified_type ?? -1
        switch verified_type {
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2, 3, 5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        // 4.VIP头像
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank <= 6 {
            vipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        // 5.处理头像
        let profileURLString = status.user?.profile_image_url ?? ""
        profileURL = NSURL(string: profileURLString)
        // 6.处理配图数据
        // 处理转发的配图
        let picURLDicts = status.pic_urls!.count != 0 ? status.pic_urls : status.retweeted_status?.pic_urls
        if let picURLDicts = picURLDicts {
            for picURLDict in picURLDicts {
                guard let picURLString = picURLDict["thumbnail_pic"] else {
                    continue
                }
                picURLs.append(NSURL(string: picURLString)!)
            }
        }
    }
}
